package com.evildog.workspace.life.application.controller;

import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.MicroHabitBO;
import com.evildog.workspace.life.application.bo.MicroHabitListBO;
import com.evildog.workspace.life.application.convert.MicroHabitConvert;
import com.evildog.workspace.life.application.model.MicroHabitListParam;
import com.evildog.workspace.life.application.model.MicroHabitParam;
import com.evildog.workspace.life.application.service.MicroHabitService;
import com.evildog.workspace.life.application.vo.MicroHabitListVO;
import com.evildog.workspace.life.application.vo.MicroHabitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * microHabit Controller
    微习惯
 * </p>
 *
 * @author evildog
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/micro-habits")
@AllArgsConstructor
@Api(tags = "microHabit相关接口")
public class MicroHabitController {

    private MicroHabitService microHabitService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody MicroHabitParam microHabitParam) {
        return ResultGenerator.status(microHabitService.save(MicroHabitConvert.INSTANCE.convert(microHabitParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(microHabitService.removeById(id));
    }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "批量删除接口")
    public Result<Boolean> removeByIds(@RequestParam("ids") List<Integer> ids) {
        return ResultGenerator.status(microHabitService.removeByIds(ids));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@RequestBody MicroHabitParam microHabitParam) {
        return ResultGenerator.status(microHabitService.updateById(id, MicroHabitConvert.INSTANCE.convert(microHabitParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<MicroHabitVO> getById(@PathVariable("id") Long id) {
        MicroHabitBO microHabitBO = microHabitService.getDetailById(id);
        return ResultGenerator.success(MicroHabitConvert.INSTANCE.convert(microHabitBO));
    }


    @GetMapping
    @ApiOperation(value = "表List查询接口")
    public Result<List<MicroHabitListVO>> getList(MicroHabitListParam microHabitListParam){
        List<MicroHabitListBO> microHabitListBO = microHabitService.getList(
                        QueryConditionUtils.getQueryCondition(microHabitListParam),
                        MicroHabitConvert.INSTANCE.convert(microHabitListParam));
        return ResultGenerator.success(MicroHabitConvert.INSTANCE.convertVoList(microHabitListBO));
    }

}